package org.xx.armory.spring5.jdbc;

import org.springframework.stereotype.Component;
import org.xx.armory.spring5.domain.Book;

import java.math.BigDecimal;
import java.sql.Types;
import java.util.Date;

@Component
public class OracleBookDao
        extends JdbcDao {
    @Override
    public void afterPropertiesSet()
            throws Exception {
        super.afterPropertiesSet();
        this.resultSetType = -10; // OracleTypes.CURSOR;
    }

    public final Book saveProc(
            long bookId,
            String title,
            String author,
            Date pubDate,
            BigDecimal price,
            int amount
    ) {
        return callAndQuerySingle(
                new org.xx.armory.spring5.jdbc.SqlAndArgsBuilder()
                        // @formatter:off
                        .append("{call SAVE_BOOK(?, ?, ?, ?, ?, ?, ?)}")
                        .addOutResult()
                        .addPositional(bookId, Types.BIGINT)
                        .addPositional(title, Types.VARCHAR)
                        .addPositional(author, Types.VARCHAR)
                        .addPositional(pubDate, Types.TIMESTAMP)
                        .addPositional(price, Types.DECIMAL)
                        .addPositional(amount, Types.INTEGER)
                // @formatter:on
                , new BookMapper()
        );
    }

    public final int insert(
            String title,
            String author,
            Date pubDate,
            BigDecimal price,
            int amount
    ) {
        return setAutoKey("BOOK_ID")
                .update(
                        new SqlAndArgsBuilder()
                                .append("INSERT INTO BOOKS")
                                .append("(BOOK_ID, TITLE, AUTHOR, PUB_DATE, PRICE, AMOUNT)")
                                .append("VALUES (BOOK_ID.NEXTVAL, #title#, #author#, #pub_date#, #price#, #amount#)")
                                .addArg("title", title, Types.VARCHAR)
                                .addArg("author", author, Types.VARCHAR)
                                .addArg("pub_date", pubDate, Types.TIMESTAMP)
                                .addArg("price", price, Types.DECIMAL)
                                .addArg("amount", amount, Types.INTEGER)
                );
    }

    public final int insertWithWrongAutoKey(
            String title,
            String author,
            Date pubDate,
            BigDecimal price,
            int amount
    ) {
        return setAutoKey("*")
                .update(
                        new SqlAndArgsBuilder()
                                .append("INSERT INTO BOOKS")
                                .append("(BOOK_ID, TITLE, AUTHOR, PUB_DATE, PRICE, AMOUNT)")
                                .append("VALUES (BOOK_ID.NEXTVAL, #title#, #author#, #pub_date#, #price#, #amount#)")
                                .addArg("title", title, Types.VARCHAR)
                                .addArg("author", author, Types.VARCHAR)
                                .addArg("pub_date", pubDate, Types.TIMESTAMP)
                                .addArg("price", price, Types.DECIMAL)
                                .addArg("amount", amount, Types.INTEGER)
                );
    }
}
